﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FFSupplier.Common.TKCommon
{
    public interface IWebAPICommon
    {
        /// <summary>
        ///  查询一条记录
        /// </summary>
        /// <param name="guid">数据唯一id</param>
        /// <param name="entityName">实体名</param>
        /// <param name="xName">
        ///第三个参数可以是空
        ///第三个参数可以是?$select=name等取具体属性
        ///第三个参数可以是？$expand=contact_customer_accounts($select=name)来取主实体属性字段及所关联的N实体记录，schemaName为1：N关系的架构名称
        ///第三个参数可以是?$expand=Account_Tasks($filter=endswith(subject,'1');$select=subject)，筛选符合条件的N实体记录</param>
        ///第三个参数可以是查询单个属性字段值 /属性名称 /new_name
        /// <returns></returns>
        Task<string> retrieveRecord(string guid, string entityNames, string xName);

        Task<string> retrieveMultipleByFilter(string entityNames, string filter);


        /// <summary>
        /// 获取相关记录
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        Task<string> retrieveMultipleByLink(string link);
        /// <summary>
        /// 分页查找
        /// </summary>
        /// <param name="weburi"></param>
        /// <param name="count"></param>
        /// <param name="auth"></param>
        /// <returns></returns>
        Task<JArray> retrieveMultiplePageing(string uri, string count, JArray jArray);

        /// <summary>
        /// 查询单个属性字段值
        /// </summary>
        /// <param name="guid">实体guid</param>
        /// <param name="entityNames">实体名</param>
        /// <param name="propertyName">属性名</param>
        /// <returns></returns>
        Task<string> retrieveOneProperty(string guid, string entityNames, string propertyName);

        /// <summary>
        /// 使用fetchxml查询数据
        /// </summary>
        /// <param name="entityName"></param>
        /// <param name="fetchxml"></param>
        /// <returns></returns>
        Task<string> retrieveRecordByFechXml(string entityNames, string fetchxml);

        /// <summary>
        /// 创建记录
        /// </summary>
        /// <param name="entityobject">实体属性集json字符串</param>
        /// <param name="entityName"></param>
        /// <returns></returns>
        Task<Guid> createRecord(JObject entityobject, string entityNames);

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="guid">实体guid</param>
        /// <param name="entityObject">需要更新的实体属性集json字符串</param>
        /// <param name="entityName">实体名</param>
        Task updateRecord(string guid, JObject entityObject, string entityNames);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="entityName"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        Task updateRecordOneProperty(string guid, string entityName, string propertyName, string propertyValue);
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="entityName"></param>
        Task deleteRecord(string guid, string entityName);

        /// <summary>
        /// 删除单个属性值
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="entityName"></param>
        /// <param name="propertyName"></param>
        Task deleteOneProperty(string guid, string entityName, string propertyName);

        /// <summary>
        /// 删除lookup字段值
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="entityName"></param>
        /// <param name="propertyName"></param>
        Task deleteLookUpProperty(string guid, string entityName, string propertyName);


        /// <summary>
        /// 绑定1：N或者N:N关系
        /// </summary>
        /// <param name="targetEntityName"></param>
        /// <param name="targetEntityId"></param>
        /// <param name="relatedEntityName"></param>
        /// <param name="relatedTargetId"></param>
        /// <param name="relatedName"></param>
        Task createNRelationShip(string targetEntityName, string targetEntityId, string relatedEntityName, string relatedTargetId, string relatedName);

        /// <summary>
        /// 删除1：N or N:N关系
        /// </summary>
        /// <param name="targetEntityName"></param>
        /// <param name="targetEntityId"></param>
        /// <param name="relatedEntityName"></param>
        /// <param name="relatedTargetId"></param>
        /// <param name="relatedName"></param>
        Task deleteNRelationShip(string targetEntityName, string targetEntityId, string relatedEntityName, string relatedTargetId, string relatedName);

        /// <summary>
        /// 将api返回得数据格式化
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="prop"></param>
        /// <returns></returns>
        string DisplayFormattedEntities(JObject entity, string prop);

        string retrieveMultipleByFilterNonAsync(string entityNames, string filter);
    }
}
